package com.example.magictowerx;

import java.io.IOException;

import android.media.MediaPlayer;

public class CMIDIPlayer
{
    public MediaPlayer	playerMusic;

    public MagicTower	magicTower	= null;


    public CMIDIPlayer(MagicTower magicTower)
    {
        this.magicTower = magicTower;

    }


    // 播放音乐
    public void PlayMusic(int ID)
    {
        FreeMusic();
        switch (ID)
        {
            case 1:
                //装载音乐
                playerMusic = MediaPlayer.create(magicTower, R.raw.menu);
                //设置循环
                playerMusic.setLooping(true);
                try
                {
                    //准备
                    playerMusic.prepare();
                }
                catch (IllegalStateException e)
                {
                    e.printStackTrace();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                //开始
                playerMusic.start();
                break;
            case 2:
                playerMusic = MediaPlayer.create(magicTower, R.raw.run);
                playerMusic.setLooping(true);
                try
                {
                    playerMusic.prepare();
                }
                catch (IllegalStateException e)
                {
                    e.printStackTrace();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                playerMusic.start();
                break;
        }
    }


    // 退出释放资源
    public void FreeMusic()
    {
        if (playerMusic != null)
        {
            playerMusic.stop();
            playerMusic.release();
        }
    }


    // 停止播放
    public void StopMusic()
    {
        if (playerMusic != null)
        {
            playerMusic.stop();
        }
    }
}
